package com.afdor.rws.event.driven.channel.rocketmq.utils;

import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

/**
 * 工具类
 *
 * @author 悭梵
 * @date Created in 2018-12-18 15:11
 */
public class RocketMqUtils {

    public static final String DEFAULT_COMPONENT_CONFIG_EXTENSION_NAME = "rocketMqTopic";


    /**
     * 主题名称
     *
     * @param eventSourceName
     * @return
     */
    public static String getTopicName(@NonNull String eventSourceName) {
        // Rocket MQ 允许字符串^[%|a-zA-Z0-9_-]+$
        return "TOPIC_" + replace(eventSourceName);
    }

    /**
     * 主题子队列名称
     *
     * @return
     */
    public static String getTopicSubQueueName(@NonNull String eventSourceName, @NonNull String consumerName) {
        // Rocket MQ 允许字符串^[%|a-zA-Z0-9_-]+$
        return "SUB_QUEUE_" + replace(eventSourceName) + "%" + replace(consumerName);
    }


    /**
     * 队列名称
     *
     * @return
     */
    public static String getQueueName(@NonNull String eventSourceName) {
        // Rocket MQ 允许字符串^[%|a-zA-Z0-9_-]+$
        return "QUEUE_" + replace(eventSourceName);
    }

    private static String replace(String value) {
        return StringUtils.replacePattern(StringUtils.replace(value, ".", "_"), "[@|#|$|%|&|*|^|!]", "-");
    }
}
